<template>
  <div class="model-form">
    <el-page-header @back="goBack" content="增加属性">
    </el-page-header>
    <div class="model-content">
      <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
        <el-row :gutter="10">
          <el-col :span="22">
            <el-form-item label="商家名称" size="mini" prop="shop">
              <el-select style="width: 100%;" v-model="form.shop" filterable clearable placeholder="请选择">
                <el-option v-for="item in shopOptions" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="22">
            <el-form-item label="属性名称" size="mini" prop="name">
              <el-input v-model="form.name" name="name" style="width: 500px;">
              </el-input>
              <el-button @click="addAttr" type="primary" class="handbt">新增属性</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item size="mini" v-for="(attr, index) in attrs" :label="'属性' + index" :key="attr.key">
              <el-input v-model="attrs[index].value" style="width: 500px;"></el-input>
              <el-button @click.prevent="removeAttr(index)" class="handbt">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right;padding-bottom: 20px;padding-top: 20px;">
            <el-button @click="goBack">取消</el-button>
            <el-button type="primary" @click="add">确定</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
  import common from "@/mixins/common.js";

  export default {
    name: "dishgroup_add",
    mixins: [common],
    data() {
      return {
        form: {
          shop: '',
          name: ''
        },
        attrs: [{
          value: ''
        }],
        rules: {
          name: [{
            required: true,
            message: '请输入属性名称',
            trigger: 'blur'
          }],
          shop: [{
            required: true,
            message: '请输入属性名称',
            trigger: 'blur'
          }]
        },
        shopOptions: []
      };
    },
    methods: {
      add() {
        var self = this;
        this.$refs["ruleForm"].validate(function (valid) {
          if (valid) {
            self.addData();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      addData() {
        var self = this,
          data = this.form;
        if (this.deforeAddData) {
          this.deforeAddData();
        }
        data.attrs = this.attrs;
        this.postJsonData("/tenantRest/dishattr/create.htm", data, function (res) {
          if (res.code == 0) {
            self.$message({
              message: '添加数据成功',
              type: 'success'
            });
            self.$router.go(-1);
          } else {
            self.$message.error(res.msg);
          }
        });
      },
      addAttr() {

        this.attrs.push({
          value: '',
          key: Date.now()
        });
      },
      removeAttr(item) {
        if (item > -1) {
          this.attrs.splice(item, 1)
        }
      }
    },
    mounted() {
      this.getShopList();
    },
  };
</script>

<style scoped>
  .handbt {
    margin-left: 10px;
  }
</style>